package com.raj.sample1;

import java.io.IOException;

import org.snmp4j.CommunityTarget;
import org.snmp4j.MessageDispatcher;
import org.snmp4j.MessageDispatcherImpl;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.mp.MPv2c;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.MultiThreadedMessageDispatcher;
import org.snmp4j.util.ThreadPool;

import com.raj.sample.SNMPReceiver;

public class SNMPSender {
	//private ConcurrentHashMap<String, HashMap<String, String>> result = new ConcurrentHashMap<String, HashMap<String, String>>();
	private Snmp snmp;
	MessageDispatcher dispatcher;
	private String threadPoolName ;
	private int threadPoolSize ;
	SNMPReceiver listener;
	
	/* public SNMPSender() {
		// TODO Auto-generated constructor stub
		 
		 }*/
		
/*	public ConcurrentHashMap<String, HashMap<String, String>> getResult() throws IOException {
		// ((MultiThreadedMessageDispatcher) dispatcher).stop();

		System.out.println("ConcurrentHashMap result :" +result);
		
		snmp.close();
		
		return result;
	}	*/

	public SNMPSender(String threadPoolName, int threadPoolSize) throws IOException {
		this.threadPoolName = threadPoolName;
		this.threadPoolSize = threadPoolSize;
		listener = new SNMPReceiver(null);
		dispatcher = new MultiThreadedMessageDispatcher(ThreadPool.create(threadPoolName, threadPoolSize), new MessageDispatcherImpl());
		snmp = new Snmp(dispatcher, new DefaultUdpTransportMapping());
		snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c());
		//snmp.setTimeoutModel(new AdvancedTimeoutModel(5000));
		snmp.listen();
		
		
	}
	
	/*private class AdvancedTimeoutModel implements TimeoutModel
	  {
	    private long retryTimeout;
	    
	    AdvancedTimeoutModel(long ioTimeout)
	    {
	      retryTimeout = ioTimeout;
	    }
	    
	    public long getRequestTimeout(int nRetries, long timeout)
	    {
	      doTraceLog("get request timeout...");
	      return retryTimeout;
	    }
	    
	    public long getRetryTimeout(int nRetries, int totalRetries, long timeout)
	    {
	      doTraceLog("get retry timeout...");
	      return 1;
	    }
	  }*/
	
/*	  public void doTraceLog(String log)
	  {
	      try
	      {
	          SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");

	          System.out.print("[" + format.format(new Date()) + "]");
	          System.out.println(log);
	      }
	      catch(Exception e)
	      {
	      }

	  }*/
	

	public void send(String  ip, String community, String mibStr) throws IOException, InterruptedException {
		//System.out.println("Receiver test");
				
		CommunityTarget target = new CommunityTarget();
		target.setVersion(SnmpConstants.version2c);
		//System.out.println("community" + community);
		target.setCommunity(new OctetString(community));
		target.setAddress(new UdpAddress(ip + "/161"));
		target.setTimeout(1500);
		target.setRetries(10);
		PDU pdu = new PDU();
		//pdu.setMaxRepetitions(10);
		pdu.setType(PDU.GET);
	    String str = mibStr;            
        String[] temp = str.split(":");
        try{
        	
        int i=0,j=0,k=3;
         for(j =0; j <temp.length ; j+=3){
         for(i =k-3; i < k ; i++){
        	if(k<=temp.length){  
           System.out.println(temp[i]);
           pdu.add(new VariableBinding(new OID(temp[i])));}
          }
          
          k+=3;
          if(!pdu.getVariableBindings().isEmpty()){
         System.out.println("snmp.send(pdu, target, null, listener)");
          snmp.send(pdu, target, new String("murali"), listener);
          }
          
          pdu.clear();
		  //break;
          }
          
        }catch(Exception e){e.printStackTrace();}
		
        //Thread.sleep(5000);
	}
	
	public void closeSNMP() throws IOException{
	snmp.close();
	}

	/*public void callback(String key, HashMap<String, String> mibRes) {
		//System.out.println("callback test");
		//this.result.put(key, mibRes);
		//System.out.println("Key :"+ key.subSequence(13,35));
		System.out.println("mibRes :"+ mibRes);
		
		//System.out.println("callback result :" + result);
	}*/
	
	
}
